package cn.zyx.org.controller;

import cn.zyx.basic.exception.BusinessException;
import cn.zyx.basic.util.AjaxResult;
import cn.zyx.basic.util.PageList;
import cn.zyx.org.domain.Shop;
import cn.zyx.org.query.ShopQuery;
import cn.zyx.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  restFul风格：是一种面向资源的架构风格
 *  在接口里面不能存在两个请求路径是一样的
 */
@RestController // 我们只会返回前端json数据
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {
    @Autowired
    private IShopService shopService;

    // 添加或修改  @PutMapping(" ")
    // @RequestBody 前后端通过json传数据，数据放到请求体里面，请求体里面拿数据必须加上 --> restFul风格必须加上
    // 以前数据是放在请求头里面的，所以不用加，但是请求头的数据长度有限制
    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){
                shopService.add(shop);
            }else{
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    // 查询一条数据 @GetMapping(" ")
    // {id} 使用花括号来占位  参数使用 @PathVariable("id") 来取值
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条店铺数据",notes = "需要传入id")
    public Shop getById(@PathVariable("id") Long id){
        return shopService.getById(id);
    }

    // 删除一条数据  @DeleteMapping(" ")
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除一条店铺数据",notes = "需要传入id")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        /*try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("系统繁忙,请稍后再试!" + e.getMessage());
        }*/
        // 链式编程 每一步操作都返回当前操作的对象
        try {
            shopService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    // 分页查询 @PostMapping(" ")
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "需要传入高级查询需要的参数")
    public PageList<Shop> queryData(@RequestBody ShopQuery shopQuery){
        return shopService.queryPage(shopQuery);
    }

    // 批量删除,如果前端传递的是数组，那么后端可以使用list或者array接收
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "需要传入ids")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "查询所有")
    public List<Shop> getAll(){
        return shopService.getAll();
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settledIn(@RequestBody Shop shop){
        try {
            shopService.settledIn(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙,请稍后再试!" + e.getMessage());
        }
    }

}
